/**
 * 
 */
package component.crawler.htmlunit;

import java.io.IOException;
import java.net.MalformedURLException;
import java.security.AccessControlException;
import java.security.GeneralSecurityException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;


/**
 * @author bliu
 *
 */
public class WebClientBase {
	WebClient webClient = null;
	

	
	public void initWebClient(){
		webClient= new WebClient(BrowserVersion.FIREFOX_3_6);
		
		webClient.setRedirectEnabled(Boolean.TRUE);
		webClient.setJavaScriptEnabled(Boolean.TRUE);
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		webClient.setActiveXNative(true);
		webClient.waitForBackgroundJavaScript(600*1000);  
		
		webClient.setThrowExceptionOnScriptError(false);  
		webClient.setThrowExceptionOnFailingStatusCode(false);  
		webClient.setTimeout(3600*1000);  
		  
		try {
			webClient.setUseInsecureSSL(true);
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		}
		webClient.setJavaScriptEngine(new JavaScriptEngine(webClient));
		
		
	}
	
	
	public void login(String loginURL, String username, String password,
			String variableNameUsername, String variableNamePassword) throws Exception{
		HtmlPage loginPage = null;
		loginPage = (HtmlPage) this.webClient.getPage(loginURL);

		loginPage = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
		HtmlForm form = (HtmlForm) loginPage.getElementById("gaia_loginform");

		if (form != null) {
		} else {
			throw new AccessControlException(
					"Login failed. the login form is not found. Please check if login UI is changed.");
		}

		HtmlTextInput accountInput = form.getElementById(variableNameUsername);
		accountInput.setValueAttribute(username);

		HtmlPasswordInput passwordInput = form.getElementById(variableNamePassword);
		passwordInput.setValueAttribute(password);

		HtmlInput submitButton = form.getElementById("signIn");
		loginPage = (HtmlPage) submitButton.click();

		Thread.sleep(1000 * 2);

		loginPage = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();

		if (loginPage.asText().contains("You don't yet have a public profile. Learn more")) {
			throw new AccessControlException(
					"Login failed. Please check username and password.");
		} else {
			
		}

	}
	
	public void crawlPage(HtmlPage advertisersPage ) throws FailingHttpStatusCodeException, MalformedURLException, IOException{

		HtmlDivision div = advertisersPage.getFirstByXPath("/html/body/div[23]/div[2]/div[2]/div/div/div/div/div[2]/div[4]/div/div/div[2]/div/div/div/div/div[2]");
		div.click();
		
		HtmlDivision  gwtdiv = (HtmlDivision)advertisersPage.getElementById("gwt-uid-1437");
		gwtdiv.click();
		HtmlInput input = advertisersPage.getFirstByXPath("/html/body/div[23]/div[2]/div[2]/div/div/div/div/div[2]/div[4]/div/div/div[3]/div/table/tbody/tr[2]/td[2]/div/div/div/input");
		HtmlInput input2 = advertisersPage.getFirstByXPath("/html/body/div[23]/div[2]/div[2]/div/div/div/div/div[2]/div[4]/div/div/div[3]/div/table/tbody/tr[2]/td[2]/div/div/div/input[2]");
		
		
	}
	
	public HtmlPage test()throws Exception{
		String advertisersUrl = "http://www.google.com/affiliatenetwork/c.html?repType=links#p8454:reports!behavior=Advertiser";
		
		HtmlPage advertisersPage = (HtmlPage)this.webClient.getPage(advertisersUrl).getEnclosingWindow().getEnclosedPage();
		System.out.println(advertisersPage.asText());
		System.out.println(advertisersPage.asText());
		System.out.println(advertisersPage.asText());
		return advertisersPage;
	}
	
	public static void main(String args[]){
		String loginURL = "https://www.google.com/accounts/ServiceLogin?service=affiliatenetwork&ltmpl=&continue=https%3A%2F%2Fwww.connectcommerce.com%2Fbin%2Flogin.mx&";
		String username = "GAN@valueclickbrands.com";
		String password = "cm0320vcb";
		String variableNameUsername = "Email";
		String variableNamePassword = "Passwd";
		
		WebClientBase base = new WebClientBase();
		base.initWebClient();
		try {
			base.login(loginURL, username, password, variableNameUsername, variableNamePassword);
			Thread.sleep(1000*20);
			base.crawlPage(base.test());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
